package dao.impl;

import entity.AcademyEntity;
import dao.AcademyDao;
import util.JDBCUtil;

import java.util.List;
import java.util.Map;

public class AcademyDaoImpl extends JDBCUtil implements AcademyDao {

    @Override
    public List<Map<String, Object>> getAcademyInfo(String name) {
        String sql;
        List<Map<String, Object>> mapList;
        if ("null".equalsIgnoreCase(name)) {
            sql = "select * from academy_info";
            mapList = queryForList(sql);
        } else if (name != null && !name.trim().isEmpty()) {
            sql = "select * from academy_info where academy_name like ?";
            String academyName = "%" + name + "%";
            mapList = queryForList(sql, academyName);
        } else {
            sql = "select * from academy_info";
            mapList = queryForList(sql);
        }
        return mapList;
    }

    @Override
    public int addAcademyInfo(AcademyEntity academy) {
        String sql = "insert into academy_info (id,academy_name,academy_code) values (?,?,?)";
        return insert(sql, academy.getId(), academy.getAcademyName(), academy.getAcademyCode());
    }

    @Override
    public int updateAcademyInfo(AcademyEntity academy) {
        String sql = "update academy_info set academy_name = ?,academy_code = ? where id = ?";
        return update(sql,academy.getAcademyName(),academy.getAcademyCode(),academy.getId());
    }

    @Override
    public int deleteAcademy(String academyId) {
        String sql = "delete academy_info from academy_info where id = ?";
        return delete(sql,academyId);
    }

    @Override
    public int batchDeleteAcademy(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            placeholders.append("?");
            if (i < ids.size() - 1) {
                placeholders.append(",");
            }
        }

        String sql = "delete from academy_info where id in (" + placeholders + ")";
        return delete(sql, ids.toArray());
    }

    @Override
    public Map<String, Object> getAcademyInfoById(String id) {
        String sql = "select * from academy_info where id = ?";
        return queryForList(sql,id).get(0);
    }
}
